package com.luo.chengrui.wordprint;

import cn.hutool.core.io.FileUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.plugin.table.HackLoopTableRenderPolicy;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WordPrinter {

    private void test() throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "tony");
        map.put("age", "18");
        // 读取本地磁盘图片
        map.put("image", new PictureRenderData(80, 30, "D:\\TEST\\a.png"));
        map.put("img", new PictureRenderData(80, 30, "D:\\TEST\\a.png"));
        // 通过url读取网络图片
//        map.put("picture", new PictureRenderData(200, 400, ".png", BytePictureUtils.getUrlByteArray("https://res.wx.qq.com/a/wx_fed/weixin_portal/res/static/img/1EtCRvm.png")));
//        File file = new File("D:\\TEST\\test.docx");
        File file = new File("D:\\TEST\\其他资金入账.docx");
        /*XWPFTemplate template = XWPFTemplate.compile(file).render(map);

        FileOutputStream out = new FileOutputStream(new File("D:\\TEST\\其他资金入账-print.docx"));
        template.write(out);
        out.flush();
        out.close();
        template.close();*/


        ConfigureBuilder builder = Configure.builder();
        builder.addPlugin('%', new MyPictureRenderPolicy());//把插件注册为新标签类型

        // 循环列
        builder.bind("detailList", new HackLoopTableRenderPolicy()).build();
        List<Map> detailData = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            Map<String, Object> dtl = new HashMap<>();
            dtl.put("name", "张三" + i);
            dtl.put("count", i);
            dtl.put("funSubject", "科目" + i);
            dtl.put("ecoSubject", "经济科目" + i);
            dtl.put("amount", i * 3);
            dtl.put("remark", "第" + i + "行");
            detailData.add(dtl);
        }
        map.put("detailList", detailData);
        XWPFTemplate template2 = XWPFTemplate.compile(file, builder.build()).render(map);
        FileOutputStream out2 = new FileOutputStream(new File("D:\\TEST\\其他资金入账-print2.docx"));
        template2.write(out2);
        out2.flush();
        out2.close();
        template2.close();
    }


    public static void main(String[] args) throws Exception {

        RenderData data = new RenderData();
        data.addText("handleName", "张三");
        data.addText("belongDeptName", "财务部");
        data.addText("fillTime", "2023-11-27");
        data.addText("financeNature", "预算内");
        data.addText("financeOtherType", "差旅费");
        data.addText("year", "2023");
        data.addTableRow("detailList", row -> {
            row.put("projectSubject", "出差费");
            row.put("feeNumber", "10");
            row.put("useSubject", "差旅费");
            row.put("economicSubject", "日常管理费");
            row.put("detailAmount", "1000.00");
            row.put("detailRemark", "明细账");
        });
        data.addTableRow("detailList", row -> {
            row.put("projectSubject", "出差费");
            row.put("feeNumber", "10");
            row.put("useSubject", "差旅费");
            row.put("economicSubject", "日常管理费");
            row.put("detailAmount", "1000.00");
            row.put("detailRemark", "明细账");
        });
        data.addTableRow("detailList", row -> {
            row.put("projectSubject", "出差费");
            row.put("feeNumber", "10");
            row.put("useSubject", "差旅费");
            row.put("economicSubject", "日常管理费");
            row.put("detailAmount", "1000.00");
            row.put("detailRemark", "明细账");
        });
        data.addTableRow("detailList", row -> {
            row.put("projectSubject", "出差费");
            row.put("feeNumber", "10");
            row.put("useSubject", "差旅费");
            row.put("economicSubject", "日常管理费");
            row.put("detailAmount", "1000.00");
            row.put("detailRemark", "明细账");
        });
        data.addTableRow("detailList", row -> {
            row.put("projectSubject", "出差费");
            row.put("feeNumber", "10");
            row.put("useSubject", "差旅费");
            row.put("economicSubject", "日常管理费");
            row.put("detailAmount", "1000.00");
            row.put("detailRemark", "明细账");
        });
        data.addTableRow("detailList", row -> {
            row.put("projectSubject", "出差费");
            row.put("feeNumber", "10");
            row.put("useSubject", "差旅费");
            row.put("economicSubject", "日常管理费");
            row.put("detailAmount", "1000.00");
            row.put("detailRemark", "明细账");
        });
        data.addTableRow("detailList", row -> {
            row.put("projectSubject", "出差费");
            row.put("feeNumber", "10");
            row.put("useSubject", "差旅费");
            row.put("economicSubject", "日常管理费");
            row.put("detailAmount", "1000.00");
            row.put("detailRemark", "明细账");
        });

        data.addPicture("signature1", "D:\\TEST\\A.png", 70, 40);
        data.addPicture("signature2", "D:\\TEST\\B.png", 70, 40);
        data.addPicture("signature3", "D:\\TEST\\C.png", 70, 40);

        data.addText("checkTime1", "2023-11-27");
        data.addText("checkTime2", "2023-11-27");
        data.addText("checkTime3", "2023-11-27");

        //generate("D:\\TEST\\其他资金入账.docx", "D:\\TEST\\其他资金入账-print000.docx", data);


        RenderDataFactory util = new RenderDataFactory().addText("belongComplanyName", "魔马科技有限公司").addImg("kj", "D:\\TEST\\C.png", 40, 70);
        generate("D:\\TEST\\其他资金入账.docx", "D:\\TEST\\其他资金入账-print001.docx", util.getData());

    }

    public static void generate(String templateFilePath, String targetFilePath, RenderData dataBinder) throws IOException {
        ConfigureBuilder builder = Configure.builder();
        builder.addPlugin('%', new MyPictureRenderPolicy());
        Map<String, Object> textRender = new HashMap<>();
        textRender.putAll(dataBinder.getTextRenderData());
        if (dataBinder.getPictureRenderData() != null) {
            dataBinder.getPictureRenderData().forEach((key, val) -> {
                textRender.put(key, new PictureRenderData(val.getWidth(), val.getHeight(), val.getPath()));
            });
        }
        if (dataBinder.getTableRenderData() != null) {
            dataBinder.getTableRenderData().forEach((key, val) -> {
                textRender.put(key, val);
                builder.bind(key, new HackLoopTableRenderPolicy()).build();
            });
        }
        XWPFTemplate template2 = XWPFTemplate.compile(templateFilePath, builder.build()).render(textRender);
        FileOutputStream out2 = new FileOutputStream(targetFilePath);
        template2.write(out2);
        out2.flush();
        out2.close();
        template2.close();
    }


    public static String generate(String templateName, RenderData dataBinder) throws IOException {
        String templatePath = "";
        String targetFile = "";

        if (FileUtil.exist(targetFile)) {
            FileUtil.del(targetFile);
            String pdf = "";
            FileUtil.del(pdf);
        }
        ConfigureBuilder builder = Configure.builder();
        builder.addPlugin('%', new MyPictureRenderPolicy());
        Map<String, Object> textRender = new HashMap<>();
        textRender.putAll(dataBinder.getTextRenderData());
        if (dataBinder.getPictureRenderData() != null) {
            dataBinder.getPictureRenderData().forEach((key, val) -> {
                textRender.put(key, new PictureRenderData(72, 27, val.toString()));
            });
        }
        if (dataBinder.getTableRenderData() != null) {
            dataBinder.getTableRenderData().forEach((key, val) -> {
                textRender.put(key, val);
                builder.bind(key, new HackLoopTableRenderPolicy()).build();
            });
        }
        XWPFTemplate template2 = XWPFTemplate.compile(templatePath + templateName, builder.build()).render(textRender);
        FileOutputStream out2 = new FileOutputStream(targetFile);
        template2.write(out2);
        out2.flush();
        out2.close();
        template2.close();
        return targetFile;
    }


}

